package cn.edu.jxyy.batistest.test;

import cn.edu.jxyy.batistest.mapper.CustomerMapper;
import cn.edu.jxyy.batistest.pojo.Customer;
import cn.edu.jxyy.batistest.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

public class Part15_TestSet {

    public static void  updateCustomerBySet(){
        SqlSession sqlSession = MyBatisUtils.getSession();

        Customer customer = new Customer();
        customer.setId(3);
        customer.setPhone("13311111234");

        CustomerMapper customerMapper =
                sqlSession.getMapper(CustomerMapper.class);

        int rows = customerMapper.updateCustomerBySet(customer);
        // 通过返回结果判断更新操作是否执行成功
        if (rows > 0) {
            System.out.println("您成功修改了" + rows + "条数据！");
        } else {
            System.out.println("执行修改操作失败！！！");
        }

        // 提交事务
        sqlSession.commit();
        // 关闭SqlSession
        sqlSession.close();
    }
    public static  void  updateCustomerByTrim(){
        // 获取SqlSession
        SqlSession sqlSession = MyBatisUtils.getSession();
        // 创建Customer对象，并向对象中添加数据
        Customer customer = new Customer();
        customer.setId(3);
        customer.setPhone("13311111111");
        // 执行SqlSession的更新方法，返回的是SQL语句影响的行数
        CustomerMapper customerMapper =
                sqlSession.getMapper(CustomerMapper.class);

        int rows = customerMapper.updateCustomerByTrim(customer);
        // 通过返回结果判断更新操作是否执行成功
        if (rows > 0) {
            System.out.println("您成功修改了" + rows + "条数据！");
        } else {
            System.out.println("执行修改操作失败！！！");
        }
        // 提交事务
        sqlSession.commit();
        // 关闭SqlSession
        sqlSession.close();
    }
    public static void main(String[] args){
        updateCustomerBySet();
        updateCustomerByTrim();
    }
}